//
//  DRHttpModel.m
//  DoctorRen
//
//  Created by Lemon on 2018/11/20.
//

#import "DRHttpModel.h"


@implementation DRHttpModel

-(instancetype)init{
    if ( self = [super init]) {
        self.params = @{};
        self.pageIndex = 0;
        self.url = @"";
        self.type = DRGet;
    }
    return self;
}

-(void)startRequestSuccess:(void (^)(id))succBlock andFail:(RequestFail)failBlock{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:self.params];
    if (self.pageIndex > 0) {
        dic[@"p"] = @(self.pageIndex);
    }
    self.isLoading = YES;
    switch (self.type) {
        case DRGet:
        {
            WEAKSELF
            [[DRHttpTool shared] GET:self.url andParams:dic andSucc:^(id  _Nonnull data) {
                if (succBlock) {
                    succBlock(data);
                }
                weakSelf.isLoading = NO;
            }  andFail:^(NSError * _Nonnull error) {
                if (failBlock) {
                    failBlock(error);
                }
                weakSelf.isLoading = NO;
            }];
        }
            break;
        case DRPost:
        {
            WEAKSELF
            [[DRHttpTool shared] POST:self.url andParams:dic andSucc:^(id  _Nonnull data) {
                if (succBlock) {
                    succBlock(data);
                }
                weakSelf.isLoading = NO;
            } andFail:^(NSError * _Nonnull error) {
                if (failBlock) {
                    failBlock(error);
                }
                weakSelf.isLoading = NO;
            }];
        }
            break;
    }
}

@end
